<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <link rel="stylesheet" href="/static/lib/css/layui.css" media="all">
</head>

<body>

    <blockquote class="layui-elem-quote layui-text">
        注意: 只能在自己的班级里发起签到 班级的每次签到以创建时间划分
    </blockquote>

    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>发起签到</legend>
    </fieldset>
    <form class="layui-form layui-form-pane" action="" lay-filter="newclass">
        <div class="layui-form-item">
            <label class="layui-form-label">选择班级</label>
            <div class="layui-input-block">
                {% for class in classes %}
                <input type="checkbox" name="{{ class.classname }}" title="{{ class.classname }}">
                {% endfor %}
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit="" lay-filter="form1">确定发起签到</button>
            </div>
        </div>
    </form>

    <script src="/static/lib/layui.js" charset="utf-8"></script>
    <script src="/static/lib/axios.min.js"></script>
    <script>
        layui.use(['form', 'layedit', 'laydate'], function () {
            var form = layui.form
                , layer = layui.layer
                , layedit = layui.layedit
                , laydate = layui.laydate
                , $ = layui.$
                , table = layui.table;
            var submitTime;
            //监听发起签到表单的提交
            form.on('submit(form1)', function (data) {
                /* layer.alert(JSON.stringify(data.field), {
                    title: '最终的提交信息'
                }) */
                data = data.field
                console.log(data)
                if (Object.keys(data).length === 0) {
                    layer.alert("请至少选择一个班级", {
                        title: '无法签到'
                    })
                    return false;
                }
                console.log(submitTime)
                if (submitTime == undefined) {
                    submitTime = Date.now();
                }else {
                    let now = Date.now();
                    let long = now - submitTime;
                    console.log(long)
                    if(long < 10000) {
                        layer.alert("您刚发起一次签到，请稍后再来……", {
                            title: '签到间隔太短'
                        })
                        return false;
                    } else {
                        submitTime = now;
                    }
                }
                for (let d in data) {
                    let formData = new FormData();
                    formData.append('classname', d);
                    axios({
                        method: 'post',
                        url: '../api/register',
                        data: formData,
                    })
                        .then(function (response) {
                            console.log(response.data);
                            result = response.data.result
                            if (result == "invalid") {
                                layer.msg("失败")
                            } else if (result == "no-exist-class") {
                                layer.msg("该班级名称不存在")
                            } else if (result == "created-one-register") {
                                layer.msg("成功发起签到")
                            }
                        })
                        .catch(function (error) {
                            console.log(error);
                        });
                }
                return false;
            });

        });
    </script>

</body>

</html>